package com.hpu.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import com.hpu.entity.User;
import com.hpu.util.DBhelp;
import com.hpu.util.RowMapper;

public class UserDao {

	private DBhelp<User> dbUser = new DBhelp<User>();
	
	public List<User> findAllUsers(){
		String sql = "select id,age,employtime,password,role,sex,tou_xiang,username from t_user";
		return dbUser.executeQueryForList(sql, new UserRowMapper());
	}
	public void saveUser(User user){
		String sql = "insert into t_user(id,username,password,sex,age,role,employtime)value(?,?,?,?,?,?,?)";
		dbUser.executeSQL(sql, user.getId(),user.getUsername(),user.getPassword(),user.getSex(),user.getAge(),user.getRole(),user.getEmploytime());
	}
	public User findByNamePassword(String name,String password){
		System.out.println(name);
		String sql = "select id,age,employtime,password,role,sex,tou_xiang,username from t_user where username = ? and password = ?";
		User user = dbUser.executeQueryForObject(sql, new UserRowMapper(), name,password);
		return user;
	}
	
	
	private class UserRowMapper implements RowMapper<User>{
		public User mapperRow(ResultSet rs) throws SQLException {
			User user = new User();
			user.setAge(rs.getInt("age"));
			user.setEmploytime(rs.getString("employtime"));
			user.setId(rs.getString("id"));
			user.setPassword(rs.getString("password"));
			user.setRole(rs.getString("role"));
			user.setSex(rs.getString("sex"));
			user.setTou_xiang(rs.getString("tou_xiang"));
			user.setUsername(rs.getString("username"));
			System.out.println(rs.getInt("age"));
			return user;
		}
		
		
	}
	
}
